/*
 * @descripttion: 上传文件
 * @author: cp
 * @Date: 2021-04-28 22:32:26
 */

const router = require('koa-router')();
const { loginCheck } = require('../middlewares/loginChecks');
const koaFrom = require('formidable-upload-koa');
const { saveFile } = require('../controller/utils');

router.prefix('/api/utils');

// 上传图片
router.post('/upload', loginCheck, koaFrom(), async (ctx, next) => {
  const file = ctx.req.files['file'];
  if (!file) {
    return;
  }
  const { size, path, name, type } = file;
  ctx.body = await saveFile({
    name,
    type,
    size,
    filePath: path,
  });
});

router.post('/aaa', async (ctx, next) => {
  ctx.body = {
    a: 1,
  };
});

module.exports = router;
